// 回调函数
/* 
const fs = require('fs')
fs.readFile('./package.json', (err, data) => {
  if (err)
    return console.log(err)
  data = JSON.parse(data)
  console.log(data.name)
}) */

// Promise
/* 
const fs = require('fs')
const promise = require('util').promisify

promise(fs.readFile)('./package.json')
  .then(data => {
    data = JSON.parse(data)
    console.log(data.name)
  })
  .catch(err => {
    console.log(err)
  }) */

// co + generator function + promise
/* 
const fs = require('fs')
const co = require('co')
const promise = require('util').promisify

co(function* () {
  let data = yield promise(fs.readFile)('./package.json')
  data = JSON.parse(data)
  console.log(data.name)
}) */

// async/await

const fs = require('fs')
const promise = require('util').promisify

async function readFile(path) {
  let data = await promise(fs.readFile)(path)
  data = JSON.parse(data)
  console.log(data.name)
}

readFile('./package.json')